package so.wy.servicecenter.businessbed.api;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import so.wy.servicecenter.base.exception.WebException;
import so.wy.servicecenter.base.frame.api.AbstractTokenHttp;
import so.wy.servicecenter.base.frame.api.httpexception.HttpExceptionHandler;
import so.wy.servicecenter.base.frame.web.webhand.WebExceptionHandler;
import so.wy.servicecenter.businessbed.web.TUserAppWeb;

@RestController
@Scope("request")
@RequestMapping("/rent/user")
public class TUserAppHttp extends AbstractTokenHttp {
    @Autowired
    private TUserAppWeb userTAppWeb;

    //微信登陆或注册
    @WebExceptionHandler
    @PostMapping("/login")
    public String userWxLoginOrRegister(String openId, String phone) throws WebException {
        return toHttpJsonKV("token",userTAppWeb.userWxLoginOrRegister(getIp(), openId, phone));
    }

    @HttpExceptionHandler
    @GetMapping("/info/token")
    public String getRentUserInfo(){
        return toHttpJson(userTAppWeb.getRentUserInfo(getUserId()));
    }

    /**
     * 更新用户
     * @return
     */
    @HttpExceptionHandler
    @PostMapping("/edit/token")
    public String updateUser(String name, String icon) throws WebException {
        userTAppWeb.updateUser(getUserId(), name, icon);
        return toHttpJson();
    }

    //获取微信session
    @HttpExceptionHandler
    @GetMapping("/wechat/jscode")
    public String getWxProCode2Session(String jsCode) throws WebException {
        return toHttpJson(userTAppWeb.getWxProCode2Session(jsCode));
    }

    //解密用户信息
    @HttpExceptionHandler
    @GetMapping("/wechat/decrypt")
    public String decrypt(String encryptedData, String sessionKey, String iv) throws WebException {
        return toHttpJson(userTAppWeb.decrypt(encryptedData, sessionKey, iv));
    }
}
